package functions

import (
	"fmt"

	"github.com/project-flogo/core/data/coerce"

	"github.com/project-flogo/core/data"
	"github.com/project-flogo/core/data/expression/function"
)

func init() {
	err := function.Register(&fnToKVPair{})
	if err != nil {
		panic(err)
	}
}

type fnToKVPair struct{}

func (fnToKVPair) Name() string {
	return "toKVPair"
}

func (fnToKVPair) Sig() (paramsTypes []data.Type, isVariadic bool) {
	return []data.Type{data.TypeString, data.TypeAny}, false
}

func (fnToKVPair) Eval(params ...interface{}) (interface{}, error) {
	if len(params) != 2 {
		return nil, fmt.Errorf("wrong number of parameters, expect 2, got %d", len(params))
	}
	key, err := coerce.ToString(params[0])
	if err != nil {
		return nil, err
	}
	m := map[string]interface{}{
		"key":   key,
		"value": params[1],
	}
	return m, nil
}
